package com.taxt.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.taxt.R;
import com.taxt.aop.Log;
import com.taxt.app.AppActivity;
import com.taxt.http.api.DeclareDataApi;
import com.taxt.http.api.JobDataApi;
import com.taxt.http.model.HttpData;
import com.taxt.other.SpUtils;
import com.taxt.ui.adapter.DeclareDataAdapter;
import com.taxt.ui.adapter.JobDataAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;


/**
 *    author : wq

 *    time   : 2019/04/20
 *    desc   : 收入纳税明细
 */
public final class PersonalDeclareDataActivity extends AppActivity implements  BaseAdapter.OnItemClickListener{
    private RecyclerView mJobRcv;
    DeclareDataAdapter jobDataAdapter;
    private List<DeclareDataApi.Bean.SalaryListDTO>list=new ArrayList<>();
    private static final String INTENT_KEY_IN_YEAR = "year";
    private String year;
    private SettingBar sbTotalMoney;
    private SettingBar sbDeclareTotalMoney;

    @Log
    public static void start(Context context, String year) {
        Intent intent = new Intent(context, PersonalDeclareDataActivity.class);
        intent.putExtra(INTENT_KEY_IN_YEAR, year);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.personal_declare_data_activity;
    }

    @Override
    protected void initView() {
        mJobRcv=findViewById(R.id.job_rcv);
        jobDataAdapter=new DeclareDataAdapter(this,list);
        mJobRcv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        jobDataAdapter.setOnItemClickListener(this);
        mJobRcv.setAdapter(jobDataAdapter);
        sbTotalMoney = (SettingBar) findViewById(R.id.sb_total_money);
        sbDeclareTotalMoney = (SettingBar) findViewById(R.id.sb_declare_total_money);

    }

    @Override
    protected void initData() {
        year=getIntent().getStringExtra(INTENT_KEY_IN_YEAR);
        getJobData();

    }



    public void getJobData(){
        HashMap<String,String>map=new HashMap<>();
        map.put("personId", SpUtils.decodeString("personId"));
        map.put("taxYear", year);
        EasyHttp.post(this)
                .json(map)
                .api(new DeclareDataApi()
                )
                .request(new HttpCallback<HttpData<DeclareDataApi.Bean>>(this) {

                    @Override
                    public void onStart(Call call) {
                       showDialog();
                    }

                    @Override
                    public void onEnd(Call call) {
                    }

                    @Override
                    public void onSucceed(HttpData<DeclareDataApi.Bean> data) {
                        hideDialog();
                        if (data.getData() != null) {
                            list.clear();
                            sbTotalMoney.setRightText(data.getData().getIncomeSum()+"元");
                            sbDeclareTotalMoney.setRightText(data.getData().getDeclaredTaxAmountSum()+"元");
                            list.addAll(data.getData().getSalaryList());
                            jobDataAdapter.setData(list);
                        }
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        hideDialog();
                    }
                });
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        DeclareDataApi.Bean.SalaryListDTO bean=list.get(position);
        PersonalDeclareDetailDataActivity.start(PersonalDeclareDataActivity.this,bean);
    }
}